package com.ruoyi.web.controller.system;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysWhitePaperProject;
import com.ruoyi.system.service.ISysWhitePaperProjectService;

/**
 * 白皮书基本信息Controller
 *
 * @author ruoyi
 * @date 2025-10-18
 */
@RestController
@RequestMapping("/system/project")
public class SysWhitePaperProjectController extends BaseController
{
    @Autowired
    private ISysWhitePaperProjectService sysWhitePaperProjectService;

    /**
     * 查询白皮书基本信息列表
     */
    @GetMapping("/list")
    public AjaxResult list()
    {
        List<SysWhitePaperProject> list = sysWhitePaperProjectService.selectSysWhitePaperProjectList();
        return success(list);
    }


    /**
     * 新增白皮书基本信息，返回项目ID
     */
    @Log(title = "白皮书基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysWhitePaperProject sysWhitePaperProject)
    {
        return success(sysWhitePaperProjectService.insertSysWhitePaperProject(sysWhitePaperProject));
    }

    /**
     * 获取白皮书基本信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysWhitePaperProjectService.selectSysWhitePaperProjectById(id));
    }

    /**
     * 生成白皮书PDF文件
     * @return
     */
    @PostMapping("/generatePdf")
    public ResponseEntity<byte[]> generatePdf(SysWhitePaperProject roject) {
        try {
            Long projectId = roject.getId();
            // 生成PDF文件字节数值
            byte[] pdfBytes = sysWhitePaperProjectService.generatePdfBytes(projectId);

            // 根据项目ID查询项目基本信息
            SysWhitePaperProject whitePaperProject = sysWhitePaperProjectService.selectSysWhitePaperProjectById(projectId);

            // 白皮书文件名称
            String fileName = whitePaperProject.getWhitePaperName() + ".pdf";
            String encodedFileName = encodeFileName(fileName);

            return ResponseEntity.ok()
                    .header("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"; filename*=UTF-8''" + encodedFileName)
                    .contentType(MediaType.APPLICATION_PDF)
                    .body(pdfBytes);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("生成白皮书PDF文件失败，{}",e.getMessage());
        }

       return null;
    }

    /**
     * 编码文件名，处理中文和特殊字符
     */
    private static String encodeFileName(String fileName) throws UnsupportedEncodingException {
        return URLEncoder.encode(fileName, "UTF-8")
                .replaceAll("\\+", "%20")
                .replaceAll("%28", "(")
                .replaceAll("%29", ")")
                .replaceAll("%3B", ";")
                .replaceAll("%40", "@")
                .replaceAll("%23", "#")
                .replaceAll("%26", "&");
    }
}
